package com.caregiver.watch.business.data.parser;

import com.google.protobuf.InvalidProtocolBufferException;
import com.caregiver.watch.business.data.protobuf.PeerInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 *  获取设备信息
 */
@Component
@Slf4j
public class PeerInfoDataParser implements IDataParser {

    @Override
    public Integer getOpt() {
        return null;
    }

    @Override
    public void dataParse(Long userId, String deviceId, byte[] payload) {
        PeerInfo.PeerInfoNotification peerInfoNotify = null;
        try {
            peerInfoNotify = PeerInfo.PeerInfoNotification.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }

        //hr alarm config
        PeerInfo.HrAlarmConf hrAlarmConfig = peerInfoNotify.getHrAlarmConf();
        if (hrAlarmConfig != null) {
            parseHrAlarmConfig(hrAlarmConfig);
        }

        //spo2 alarm config
        PeerInfo.spo2AlarmConfv2 spo2AlarmConfig = peerInfoNotify.getSpo2AlarmConf();
        if (spo2AlarmConfig != null) {
            parseSpo2AlarmConfig(spo2AlarmConfig);
        }

        //tmpr alarm config
        PeerInfo.TemperatureAlarmConfv2 tmprAlarmConfig = peerInfoNotify.getTempAlarmConf();
        if (tmprAlarmConfig != null) {
            parseTemperatureAlarmConfig(tmprAlarmConfig);
        }

        //bp alarm config
        PeerInfo.BpAlarmConfv2 bpAlarmConfig = peerInfoNotify.getBpAlarmConf();
        if (bpAlarmConfig != null) {
            parseBpAlarmConfig(bpAlarmConfig);
        }

        //fall check config
        boolean fall_check = peerInfoNotify.getFallCheck();
        log.info("fall off check - {}", fall_check ? "enable" : "disable");
    }

    void parseHrAlarmConfig(PeerInfo.HrAlarmConf hrAlarmConfig) {
        boolean enable = hrAlarmConfig.getEnable();
        int high = hrAlarmConfig.getThsHigh();
        int low = hrAlarmConfig.getThsLow();
        int timeout = hrAlarmConfig.getTimeout();
        int interval = hrAlarmConfig.getInterval();
        log.info("hr alarm - {} high:{},low:{},timeout:{},interval:{}", enable ? "enable" : "disable",
                high, low, timeout, interval);
    }

    void parseSpo2AlarmConfig(PeerInfo.spo2AlarmConfv2 spo2AlarmConfig) {
        boolean enable = spo2AlarmConfig.getEnable();
        int low = spo2AlarmConfig.getThsLow();
        log.info("spo2 alarm - {} low:{}", enable ? "enable" : "disable", low);
    }

    void parseTemperatureAlarmConfig(PeerInfo.TemperatureAlarmConfv2 tmprAlarmConfig) {
        boolean enable = tmprAlarmConfig.getEnable();
        int high = tmprAlarmConfig.getTempHigh();
        int low = tmprAlarmConfig.getTempBelow();
        log.info("temperature alarm - {} high:{},low:{}", enable ? "enable" : "disable", high, low);
    }

    void parseBpAlarmConfig(PeerInfo.BpAlarmConfv2 bpAlarmConfig) {
        boolean enable = bpAlarmConfig.getEnable();
        int dbp_low = bpAlarmConfig.getDbpBelow();
        int dbp_high = bpAlarmConfig.getDbpHigh();
        int sbp_low = bpAlarmConfig.getSbpBelow();
        int sbp_high = bpAlarmConfig.getSbpHigh();
        log.info("bp alarm - {} dbp: {} {},sbp:{} {}", enable ? "enable" : "disable", dbp_low, dbp_high, sbp_low, sbp_high);
    }
}
